package com.pilot.game.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.pilot.game.domain.CyclePlan;
import com.pilot.game.service.CyclePlanService;
import com.pilot.game.web.rest.errors.BadRequestAlertException;
import com.pilot.game.web.rest.util.HeaderUtil;
import com.pilot.game.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing CyclePlan.
 */
@RestController
@RequestMapping("/api")
public class CyclePlanResource {

    private final Logger log = LoggerFactory.getLogger(CyclePlanResource.class);

    private static final String ENTITY_NAME = "cyclePlan";

    private final CyclePlanService cyclePlanService;

    public CyclePlanResource(CyclePlanService cyclePlanService) {
        this.cyclePlanService = cyclePlanService;
    }

    /**
     * POST  /cycle-plans : Create a new cyclePlan.
     *
     * @param cyclePlan the cyclePlan to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cyclePlan, or with status 400 (Bad Request) if the cyclePlan has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cycle-plans")
    @Timed
    public ResponseEntity<CyclePlan> createCyclePlan(@RequestBody CyclePlan cyclePlan) throws URISyntaxException {
        log.debug("REST request to save CyclePlan : {}", cyclePlan);
        if (cyclePlan.getId() != null) {
            throw new BadRequestAlertException("A new cyclePlan cannot already have an ID", ENTITY_NAME, "idexists");
        }
        CyclePlan result = cyclePlanService.save(cyclePlan);
        return ResponseEntity.created(new URI("/api/cycle-plans/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cycle-plans : Updates an existing cyclePlan.
     *
     * @param cyclePlan the cyclePlan to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cyclePlan,
     * or with status 400 (Bad Request) if the cyclePlan is not valid,
     * or with status 500 (Internal Server Error) if the cyclePlan couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cycle-plans")
    @Timed
    public ResponseEntity<CyclePlan> updateCyclePlan(@RequestBody CyclePlan cyclePlan) throws URISyntaxException {
        log.debug("REST request to update CyclePlan : {}", cyclePlan);
        if (cyclePlan.getId() == null) {
            return createCyclePlan(cyclePlan);
        }
        CyclePlan result = cyclePlanService.save(cyclePlan);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, cyclePlan.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cycle-plans : get all the cyclePlans.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of cyclePlans in body
     */
    @GetMapping("/cycle-plans")
    @Timed
    public ResponseEntity<List<CyclePlan>> getAllCyclePlans(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of CyclePlans");
        Page<CyclePlan> page = cyclePlanService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/cycle-plans");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /cycle-plans : get all the cyclePlans.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of cyclePlans in body
     */
    @GetMapping("/cycle-plans/withWin")
    @Timed
    public ResponseEntity<List<CyclePlan>> getAllCyclePlansWithStatus(@ApiParam Pageable pageable,@RequestParam int status,
                                                                      @RequestParam int kindId) {
        log.debug("REST request to get a page of CyclePlans");
        Page<CyclePlan> page = cyclePlanService.findAllWithStatus(kindId, status, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/cycle-plans");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /cycle-plans/:id : get the "id" cyclePlan.
     *
     * @param id the id of the cyclePlan to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cyclePlan, or with status 404 (Not Found)
     */
    @GetMapping("/cycle-plans/{id}")
    @Timed
    public ResponseEntity<CyclePlan> getCyclePlan(@PathVariable Long id) {
        log.debug("REST request to get CyclePlan : {}", id);
        CyclePlan cyclePlan = cyclePlanService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(cyclePlan));
    }

    /**
     * DELETE  /cycle-plans/:id : delete the "id" cyclePlan.
     *
     * @param id the id of the cyclePlan to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cycle-plans/{id}")
    @Timed
    public ResponseEntity<Void> deleteCyclePlan(@PathVariable Long id) {
        log.debug("REST request to delete CyclePlan : {}", id);
        cyclePlanService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
